<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容-头部 -->
      <el-card class="tree-card">
        <!-- 防止结构内容 -->
        <tree-tools :tree-node="company" :is-root="true"></tree-tools>
        <el-tree
          :data="departs"
          :props="defaultProps"
          @addDepts="addDepts"
          :default-expand-all="true"
        >
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <tree-tools
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDept="editDept"
            slot-scope="{ data }"
            :tree-node="data"
          ></tree-tools>
        </el-tree>
      </el-card>
    </div>
    <add-dept
      :tree-node="node"
      :showDialog.sync="showDialog"
      @addDepts="getDepartments"
      ref="addDept"
    ></add-dept>
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools.vue";
import AddDept from "./components/add-dept.vue";
import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils";
export default {
  components: {
    TreeTools,
    AddDept,
  },
  data() {
    return {
      company: { name: "", manager: "负责人" },
      defaultProps: {
        label: "name",
      },
      departs: [],
      showDialog: false,
      node: null,
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      let res = await getDepartments();
      this.company = { name: res.companyName, manager: "负责人" };
      this.departs = tranListToTreeData(res.depts, ""); //需要转化为树形结构
      // console.log(res);
    },
    addDepts(node) {
      this.showDialog = true;
      this.node = node;
    },
    editDept(node) {
      this.showDialog = true;
      this.node = node;
      this.$refs.addDept.getDepartDetail(node.id);
    },
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
